//
//  MineViewController.m
//  移动营销
//
//  Created by Hanen 3G 01 on 16/2/23.
//  Copyright © 2016年 Hanen 3G 01. All rights reserved.
//

#import "MineViewController.h"
#import "MineCell.h"
#import "BusinessAdressBookVC.h"
#import "InviteViewController.h"
#import "SettingViewController.h"
#import "InformationViewController.h"
#import "QRCodeGenerator.h"
#import "FXBlurView.h"

#define cellHeight 50.0f

@interface MineViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    NSArray     *_imageArray;
    NSArray     *titleArray;
    UIImageView *_bgImage1;
    UILabel     *qrNameLabel;
    UIButton    *QRbutton;
}
@property(nonatomic,strong)NSDictionary   *dictionary;
@property(nonatomic,strong)UIView         *qrView;
@property(nonatomic,strong)UIImageView    *qrImageView;
@property(nonatomic,strong)UIImageView    *bgImage;

@end

@implementation MineViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = graySectionColor;
    self.navigationController.navigationBarHidden = YES;
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    _dictionary = [[NSDictionary alloc]init];
    
    
    [self createUI];
    
    [self createQRview];
    
//    [self initData];
    
}
- (void)initData{
    
    NSDictionary * dic =@{@"token":TOKEN,@"uid":@(UID)};
    [DataTool sendGetWithUrl:MY_HOMEPAGE_URL parameters:dic success:^(id data) {
        NSDictionary *dic = [DataTool changeType:[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil]];
        id jsonDic = [DataTool changeType:dic];
        NSLog(@"%@+++++--wodezhuye",jsonDic);
        if(jsonDic){
            
            _dictionary = jsonDic;
            NSLog(@"%@  dic+++++",self.dictionary);
            NSString *namString = jsonDic[@"name"];
            if(namString != nil){
                _nameLabel.text = namString;
            }else{
                _nameLabel.text = @"";
            }
            
            NSString  *imageStr =[NSString stringWithFormat:@"%@%@",LoadImageUrl,jsonDic[@"logo"]];
            [_headerBtn sd_setBackgroundImageWithURL:[NSURL URLWithString:imageStr] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"HY_btn_edit_head"]];
            
            
            [_bgImage1 sd_setImageWithURL:[NSURL URLWithString:imageStr]];
            
            _phoneLabel.text = [NSString stringWithFormat:@"手机号:%@",jsonDic[@"username"]];
            
            NSString  *qrImageStr =[NSString stringWithFormat:@"%@%@",LoadImageUrl,jsonDic[@"img"]];
            _qrImageView.image = [QRCodeGenerator qrImageForString:qrImageStr imageSize:300];
            
        }
        
    } fail:^(NSError *error) {
        NSLog(@"%@+++++++++",error);
    }];
}

- (void)createUI{
    
    _imageArray = [[NSArray alloc]initWithObjects:@"HY_list_icon_contacts",@"HY_list_icon_invitation",@"HY_list_icon_set", nil];
    titleArray = @[@"企业通讯录",@"邀请",@"设置"];
    
    CGFloat bgImageH     = [UIView getHeight:270];//背景图片的高度
    CGFloat headerImageW  = [UIView getHeight:120];//头像尺寸
    CGFloat qrSpace      = 15;
    CGFloat qrW          = 20;
    
    UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KSCreenW, bgImageH)];
    headerView.backgroundColor = NavBackColor;
    [self.view addSubview:headerView];
    
    UILabel *myLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 20+(44-20)/2, KSCreenW, 20)];
    myLabel.text = @"我的";
    myLabel.textColor = [UIColor whiteColor];
    myLabel.font = [UIFont systemFontOfSize:18];
    myLabel.textAlignment = NSTextAlignmentCenter;
    [headerView addSubview:myLabel];
    
    _qrBtn = [[UIButton alloc]initWithFrame:CGRectMake(KSCreenW - qrW - qrSpace-5, 20+(44-20)/2, qrW, qrW)];
    [_qrBtn setImage:[UIImage imageNamed:@"HY_nav_icon_QR"] forState:UIControlStateNormal];
    [_qrBtn addTarget:self action:@selector(getQRcode:) forControlEvents:UIControlEventTouchUpInside];
    [headerView addSubview:_qrBtn];
    
    _headerBtn = [[UIButton alloc]initWithFrame:CGRectMake((KSCreenW-headerImageW)/2, (headerView.height - headerImageW)/2, headerImageW, headerImageW)];
    _headerBtn.backgroundColor = [UIColor cyanColor];
    _headerBtn.layer.cornerRadius = headerImageW/2;
    _headerBtn.layer.masksToBounds = YES;
    [_headerBtn.layer setBorderWidth:5.0];
    [_headerBtn.layer setBorderColor:UIColorFromRGB(0x8095c4).CGColor];//边框颜色
    [_headerBtn addTarget:self action:@selector(clickToMyInformation:) forControlEvents:UIControlEventTouchUpInside];
    [headerView addSubview:_headerBtn];
    
    _nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, _headerBtn.maxY + 10, KSCreenW, 20)];
    _nameLabel.textColor = UIColorFromRGB(0xc4cbdf);
    _nameLabel.textAlignment = NSTextAlignmentCenter;
    _nameLabel.font = [UIFont systemFontOfSize:16.0f];
    [headerView addSubview:_nameLabel];
    
    
    _phoneLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, _nameLabel.maxY +5, KSCreenW, 20)];
    _phoneLabel.textColor=UIColorFromRGB(0xc4cbdf);
    _phoneLabel.textAlignment = NSTextAlignmentCenter;
    _phoneLabel.font = [UIFont systemFontOfSize:14.0f];
    [headerView addSubview:_phoneLabel];
    
    
    UIView *lineView = [ViewTool getLineViewWith:CGRectMake(0, headerView.maxY + 20 -1, KSCreenW, 1) withBackgroudColor:grayLineColor];
    [self.view addSubview:lineView];
    
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, lineView.maxY, KSCreenW, 3*cellHeight) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.scrollEnabled =NO;
    
    _tableView.rowHeight = cellHeight;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:_tableView];
    
}
#pragma mark------弹出的二维码界面
- (void)createQRview{
    
    UIWindow *window = [UIApplication sharedApplication].delegate.window;
    
    _qrView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KSCreenW, KSCreenH)];
    _qrView.backgroundColor = [UIColor blackColor];
    _qrView.alpha = 0.5;
    _qrView.hidden = YES;
    [window addSubview:_qrView];
    
    _bgImage = [[UIImageView alloc]initWithFrame:CGRectMake([UIView getWidth:60], [UIView getHeight:120], KSCreenW - 2*[UIView getWidth:60], KSCreenW - 2*[UIView getWidth:60])];
    //    _bgImage.image = [UIImage imageNamed:@"二维码bg"];
    _bgImage.backgroundColor = [UIColor whiteColor];
    _bgImage.hidden = YES;
    _bgImage.layer.cornerRadius = 5;
    _bgImage.layer.masksToBounds = YES;
    [window addSubview:_bgImage];
    
    QRbutton = [[UIButton alloc]initWithFrame:CGRectMake((KSCreenW -40) / 2, _bgImage.maxY + 50, 40, 40)];
    [QRbutton setImage:[UIImage imageNamed:@"HY_btn_close"] forState:UIControlStateNormal];
    [QRbutton addTarget:self action:@selector(hiddenQRView) forControlEvents:UIControlEventTouchUpInside];
    QRbutton.hidden = YES;
    [window addSubview:QRbutton];
    
    qrNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, [UIView getHeight:20], KSCreenW - 2*[UIView getWidth:40], [UIView getHeight:30])];
    qrNameLabel.textColor = [UIColor whiteColor];
    qrNameLabel.textAlignment = NSTextAlignmentCenter;
    [_bgImage addSubview:qrNameLabel];
    
    _qrImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, _bgImage.width, _bgImage.height)];
    [_bgImage addSubview:_qrImageView];
    
    
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(toHiddenQRView:)];
    [_qrView addGestureRecognizer:tapGesture];
    
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [self initData];
    
    self.navigationController.navigationBarHidden = YES;
    
}
#pragma mark------ 二维码按钮
- (void)getQRcode:(UIButton *)btn{
    [UIView animateWithDuration:0.25 animations:^{
        _qrView.hidden = NO;
        _bgImage.hidden = NO;
        QRbutton.hidden = NO;
    }];
    
}
- (void)hiddenQRView{
    [UIView animateWithDuration:0.25 animations:^{
        _qrView.hidden = YES;
        _bgImage.hidden = YES;
        QRbutton.hidden = YES;
    }];
}
- (void)toHiddenQRView:(UITapGestureRecognizer *)tap{
    [UIView animateWithDuration:0.25 animations:^{
        _qrView.hidden = YES;
        _bgImage.hidden = YES;
        QRbutton.hidden = YES;
    }];
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [UIView animateWithDuration:0.25 animations:^{
        _qrView.hidden = YES;
        _bgImage.hidden = YES;
    }];
}
#pragma mark------进入我的资料
- (void)clickToMyInformation:(UIButton *)btn{
    InformationViewController *vc = [[InformationViewController alloc]init];
    
    vc.name = [DataTool changeType:_dictionary[@"name"]];
    vc.imag = [DataTool changeType:_dictionary[@"img"]];
    vc.phone = [DataTool changeType:_dictionary[@"username"]];
    vc.logo = [DataTool changeType:_dictionary[@"logo"]];
    vc.department = [DataTool changeType:_dictionary[@"departname"]];
    vc.position = [DataTool changeType:_dictionary[@"positionname"]];
    vc.type = [DataTool changeType:_dictionary[@"type"]];
    
    vc.positionID = (int)[_dictionary[@"position"]integerValue];
    vc.departID = (int)[_dictionary[@"departid"]integerValue];
    vc.uID = (int)[_dictionary[@"uid"] integerValue];
    
    [self.navigationController pushViewController:vc animated:YES];
    
}
#pragma mark
#pragma mark---------tableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _imageArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cell";
    MineCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell=[[MineCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    if (indexPath.row == 2) {
        cell.lineView.frame = CGRectMake(0, cellHeight - 1, KSCreenW, 1);
    }
    cell.headerImage.image = [UIImage imageNamed:_imageArray[indexPath.row]];
    cell.rightImg.image = [UIImage imageNamed:@"HY_list_icon_arrow"];
    cell.titleLabel.text = titleArray[indexPath.row];
    [ViewTool setLableFont14:cell.titleLabel];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    switch (indexPath.row) {
        case 0:
        {BusinessAdressBookVC *vc = [[BusinessAdressBookVC alloc]init];
            [self.navigationController pushViewController:vc animated:YES];}
            break;
        case 1:
        { InviteViewController *vc = [[InviteViewController alloc]init];
            [self.navigationController pushViewController:vc animated:YES];}
            break;
        case 2:
        {SettingViewController *vc = [[SettingViewController alloc]init];
            [self.navigationController pushViewController:vc animated:YES];}
            break;
        default:
            break;
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
